--
-- Author: ping.wang
-- Date: 2018-08-13 20:00:06
--
-- RewardMonthSignPanel 30天签到的界面
--

local ClassRef = app.mvc.createPanelClass(...)
local RewardMonthSignRender = import(".renders.RewardMonthSignRender")
local table_insert = table.insert

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.RewardMonthSignPanel))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize( self._ui.Panel:getContentSize() )
    self._icon = self._ui.icon
    self._tlt = self._ui.tlt
    self._tltBg = self._ui.bg02
    self._Text = self._ui.Text_1
    self._traceBtn = self._ui.selected_btn --追踪按钮

    self._panelDate = self._ui.Panel_date --内容面板

    self._allRenders = {}

    local panelHeight = self._panelDate:getContentSize().height

    for k = 1,25 do
        local render = RewardMonthSignRender.create()
        render:setAnchorPoint(0,1)
        local size = render:getContentSize()
        local i = math.floor((k - 1) / 5)
        local j = (k - 1) % 5
        render:setPosition(size.width * j,panelHeight - size.height * i)
        self._panelDate:addChild(render)
        table_insert(self._allRenders,render)
    end

    self._traceBtn:addClickEventListener(function()
        local isSelected = self._traceBtn:isBright()
        self._traceBtn:setBright(not isSelected)
        --需要发送是否追踪的消息
        executeFunc(self._onOperate, "changeTraceStatus", {self._data.uuid, isSelected})
    end)
    self._qqBtn1 = self._ui["Panel/bg_qq_1"]
    self._qqBtn2 = self._ui["Panel/bg_qq_2"]
    self._wxBtn = self._ui["Panel/bg_wx"]
    self._qqBtn1:setTouchEnabled(true)
    self._qqBtn2:setTouchEnabled(true)
    self._wxBtn:setTouchEnabled(true)
    self._qqBtn1:addClickEventListener(handler(self, self._onClickQQGameWakeUp))
    self._qqBtn2:addClickEventListener(handler(self, self._onClickQQVip))
    self._wxBtn:addClickEventListener(handler(self, self._onClickWXGameWakeUp))
    self._singleMsdkBtnPos = self._wxBtn:getPositionX() + 15
	self:refreshMsdk()
   
    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self:setVisible( true )

        local data = self._data
        self._icon:loadTexture(Res.activityIconPath..data.iconName..".png")
        self._tlt:setString(L(data.name))

        assert(#data.cell <= #self._allRenders,"celldata must <= 25")
        local totalNum = 0
        for k,v in ipairs(data.cell) do
            if k <= #self._allRenders then
                self._allRenders[k]:setData(v)
                self._allRenders[k]:setCanGet(k <= data.curIdx)
                self._allRenders[k]:setActivityId(data.uuid)
                self._allRenders[k]:onBtnClick(self._onOperate)
                if v.haveGet then
                    totalNum = totalNum + 1
                end
            end
        end
        self._Text:setString(string.format(L(data.des),totalNum))
        self._traceBtn:setVisible(not data.isDefaultTrace)
        self._traceBtn:setBright(not data.isTrace)
    else
        self:setVisible( false )
    end
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

function ClassRef:setData(data)
    self._data = data
    self:refresh()
end

function ClassRef:refreshMsdk()
    local msdkData = UD.msdkData
    local platform = app.channelAPI:getWGPlatform()
    if platform == Enums.MSDK_PLATFORM.QQ then
        local isWakeUpQQ = msdkData:isMyWakeUpFromQQGame() 
        local isVip = msdkData:isVip()
        if isWakeUpQQ and isVip then
            self._qqBtn1:setVisible(true)
            self._qqBtn2:setVisible(true)
        else
            if isWakeUpQQ then
                self._qqBtn1:setVisible(true)
                self._qqBtn1:setPositionX(self._singleMsdkBtnPos)
                self._qqBtn2:setVisible(false)
            elseif isVip then
                self._qqBtn1:setVisible(false)
                self._qqBtn2:setVisible(true)
                self._qqBtn2:setPositionX(self._singleMsdkBtnPos)
            else
                self._qqBtn2:setVisible(false)
                self._qqBtn1:setVisible(false)
            end
        end
    elseif platform == Enums.MSDK_PLATFORM.WX  then
        self._qqBtn2:setVisible(false)
        self._qqBtn1:setVisible(false)
        self._wxBtn:setVisible(msdkData:isMyWakeUpFromWXGame())
    else
        self._qqBtn2:setVisible(false)
        self._qqBtn1:setVisible(false)
        self._wxBtn:setVisible(false)
    end
end

function ClassRef:_onClickQQVip()

end

function ClassRef:_onClickQQGameWakeUp()

end

function ClassRef:_onClickWXGameWakeUp()

end


return ClassRef
